package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import dm.jdbc.util.StringUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.AutomodelsDao;
import io.renren.dto.AutomodelsDTO;
import io.renren.dto.CarInfoDTO;
import io.renren.dto.CarTypeDto;
import io.renren.entity.AutokindsEntity;
import io.renren.entity.AutomodelsEntity;
import io.renren.service.AutomodelsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-27
 */
@Service
public class AutomodelsServiceImpl extends CrudServiceImpl<AutomodelsDao, AutomodelsEntity, AutomodelsDTO> implements AutomodelsService {

    @Override
    public QueryWrapper<AutomodelsEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutomodelsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public BigDecimal selectMinOneByAutoGroupFK(String autogroupoid) {
        return baseDao.selectMinOneByAutoGroupFK(autogroupoid);
    }

    @Override
    public BigDecimal selectMaxOneByAutoGroupFK(String autogroupoid) {
        return baseDao.selectMaxOneByAutoGroupFK(autogroupoid);
    }

    @Override
    public List<Map<String, Object>> getCarYearList(String autoGroupFk) {
        List<Map<String, Object>> resList = new ArrayList<>();
        QueryWrapper<AutomodelsEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoModelsOID,AutoYear");
        wrapper.eq("AutoGroup_FK", autoGroupFk);
        wrapper.apply("(StopDate is  null or StopDate = '1900-01-01')");
        List<AutomodelsEntity> list = baseDao.selectList(wrapper);
        if (!list.isEmpty()) {

            list = list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AutomodelsEntity::getAutoyear))), ArrayList<AutomodelsEntity>::new)
            );
            int indexNo = 1;
            for (AutomodelsEntity item : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("AutoModelsOID", item.getAutomodelsoid());
                map.put("AutoYear", item.getAutoyear());
                map.put("ROW_NUMBER", indexNo + "");
                resList.add(map);
                indexNo++;
            }
            return resList;
        }
        return Collections.emptyList();
    }

    @Override
    public List<Map<String, Object>> getCarDisplacementList(String autoGroupFk, String autoYear) {
        List<Map<String, Object>> resList = new ArrayList<>();
        QueryWrapper<AutomodelsEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoModelsOID, AutoDisplacement");
        wrapper.eq("AutoGroup_FK", autoGroupFk);
        if (StringUtil.isNotEmpty(autoYear)) {
            wrapper.eq("AutoYear", autoYear);
        }
        wrapper.apply("(StopDate is  null or StopDate = '1900-01-01')");
        List<AutomodelsEntity> list = baseDao.selectList(wrapper);
        if (!list.isEmpty()) {
            list = list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AutomodelsEntity::getAutodisplacement))), ArrayList<AutomodelsEntity>::new)
            );
            int indexNo = 1;
            for (AutomodelsEntity item : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("AutoModelsOID", item.getAutomodelsoid());
                map.put("AutoDisplacement", item.getAutodisplacement());
                map.put("ROW_NUMBER", indexNo + "");
                resList.add(map);
                indexNo++;
            }
            return resList;
        }
        return Collections.emptyList();
    }

    @Override
    public List<Map<String, Object>> getCarConfigList(String autoGroupFk, String autoYear) {
        List<Map<String, Object>> resList = new ArrayList<>();
        QueryWrapper<AutomodelsEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoModelsOID,Name");
        wrapper.eq("AutoGroup_FK", autoGroupFk);
        if (StringUtil.isNotEmpty(autoYear)) {
            wrapper.eq("AutoYear", autoYear);
        }
        wrapper.apply("(StopDate is  null or StopDate = '1900-01-01')");
        List<AutomodelsEntity> list = baseDao.selectList(wrapper);

        if (!list.isEmpty()) {
            int indexNo = 1;
            for (AutomodelsEntity item : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("AutoModelsOID", item.getAutomodelsoid());
                map.put("Name", item.getName());
                map.put("ROW_NUMBER", indexNo + "");
                resList.add(map);
                indexNo++;
            }
            return resList;
        }
        return Collections.emptyList();
    }

    @Override
    public AutomodelsEntity findOneByAutoModelsOID(String automodelsFk) {
        QueryWrapper<AutomodelsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoModelsOID", automodelsFk);
        List<AutomodelsEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<Map<String, Object>> carTypeList(CarTypeDto dto) {
        List<Map<String, Object>> resList = baseDao.selectMaps(
                new QueryWrapper<AutomodelsEntity>()
                        .select("AutoModelsOID,Name")
                        .eq("AutoGroup_FK", dto.getAutoGroupFk())
                        .eq("AutoYear", dto.getAutoYear())
                        .eq("AutoDisplacement", dto.getAutoDisplacement())
                        .apply("(StopDate is null or StopDate = '1900-01-01')")
                        .orderByAsc("Name"));
        return resList;
    }
}